package com.example.myandroidapp1;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myandroidapp1.activity.AbstractBaseActivity;

public class ExternalData extends AbstractBaseActivity implements
		OnItemSelectedListener,OnClickListener {

	private TextView canWrite;
	private TextView canRead;
	private String state;
	private boolean canR;
	private boolean canW;
	private Spinner spinner;
	String[] paths = { "Music", "Pictures", "Downloads" };
	File path = null;
	File file = null;
	
	
	private EditText etSaveFile;
	private Button bConfirm;
	private Button bSave;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.externaldata);
		canWrite = (TextView) findViewById(R.id.tvCanWrite);
		canRead = (TextView) findViewById(R.id.tvCanRead);
		
		etSaveFile = (EditText ) findViewById(R.id.etSaveAs);
		bSave = (Button) findViewById(R.id.bSaveExternalData);
		bConfirm = (Button) findViewById(R.id.bConfirmExternalData);
		bSave.setOnClickListener(this);
		bConfirm.setOnClickListener(this);
		
		checkState();

		canWrite.setText("NONE");
		canRead.setText("NONE");
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, paths);
		spinner = (Spinner) findViewById(R.id.spinnerExternalData);
		spinner.setAdapter(adapter);
		spinner.setOnItemSelectedListener(this);
	}

	private void checkState() {
		// TODO Auto-generated method stub
		state = Environment.getExternalStorageState();
		if (state != null && state.equals(Environment.MEDIA_MOUNTED)) {
			// read and write
			canWrite.setText("true");
			canRead.setText("true");
			canR = canW = true;
		} else if (state != null
				&& state.equals(Environment.MEDIA_MOUNTED_READ_ONLY)) {
			// read but cannot write
			canWrite.setText("false");
			canRead.setText("true");
			canW = false;
			canR = true;
		} else {
			canWrite.setText("false");
			canRead.setText("false");
			canR = canW = false;
		}
	}

	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		int position = spinner.getSelectedItemPosition();
		switch (position) {
		case 0:
			path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
			break;

		case 1:
			path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
			break;
		case 2:
			path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
			break;
	
		default:
			break;
		}
	}

	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.bSaveExternalData:
			path.mkdirs();
			String f = etSaveFile.getText().toString();
			file = new File(path,f + ".png");
			checkState();
			
			if(canW == canR == true){
				try {
					InputStream is = getResources().openRawResource(R.drawable.green_ball);
					OutputStream os = new FileOutputStream(file);
					byte[] data = new byte[is.available()];
					is.read(data);
					os.write(data);
					is.close();
					os.close();
					Toast toast = Toast.makeText(this, "File has been saved!!!", Toast.LENGTH_LONG);
					toast.show();
					
					//update files for the users to use
					MediaScannerConnection.scanFile(this, new String[] {file.toString()}, null, new MediaScannerConnection.OnScanCompletedListener() {
						
						@Override
						public void onScanCompleted(String path, Uri uri) {
							// TODO Auto-generated method stub
							Toast t = Toast.makeText(ExternalData.this, "Scan Complete", Toast.LENGTH_LONG);
							t.show();
						}
					});
					
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			break;
		case R.id.bConfirmExternalData:
			bSave.setVisibility(View.VISIBLE);
			break;

		default:
			break;
		}
	}
}
